using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace TST.SharePoint.Shared.WebControls
{
    /// <summary>
    /// Template object for a column. To be used in a Grid object. 
    /// Render a non-clickable checkbox, based on a boolean value in the datasource.
    /// </summary>
    public class CurrencyColumn : ITemplate
    {
        private string _fieldName;
        private int _culture;
        private string _formatting;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="boolFieldName">The field name of the boolean field.</param>
        public CurrencyColumn(string fieldName, string formatting)
        {
            _fieldName = fieldName;
            _formatting = formatting;
        }

        public CurrencyColumn(string fieldName, int culture, string formatting)
        {
            _fieldName = fieldName;
            _culture = culture;
            _formatting = formatting;
        }

        public void InstantiateIn(Control container)
        {
            if (container != null)
            {
                Label text = new Label();
                text.ID = string.Concat("CurrencyField_", _fieldName);
                text.DataBinding += new EventHandler(text_DataBinding);
                container.Controls.Add(text);
            }
        }

        void text_DataBinding(object sender, EventArgs e)
        {
            Label text = (Label)sender;
            if (!string.IsNullOrEmpty(_fieldName))
            {
                GridViewRow container = (GridViewRow)text.NamingContainer;
                if (((System.Data.DataRowView)container.DataItem)[_fieldName] != System.DBNull.Value)
                {
                    double value;
                    object val = ((System.Data.DataRowView)container.DataItem)[_fieldName];
                    if (val is string)
                    {
                        // string is the result type for calculated fields with output type Currency.
                        double.TryParse(val.ToString(), out value);
                    }
                    else
                    {
                        value = (double)((System.Data.DataRowView)container.DataItem)[_fieldName];
                    }

                    if (string.IsNullOrEmpty(_formatting))
                        _formatting = "C";
                    if (_culture > 0)
                    {
                        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(_culture);
                        text.Text = value.ToString(_formatting, culture);
                    }
                    else
                    {
                        text.Text = value.ToString(_formatting);
                    }
                }
            }
        }

    }
}
